package com.tuanzi.loan.web.bean.statistics;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.service.common.CommProductIdService;
import com.tuanzi.loan.business.service.statistics.ActivityAverageJoinStatService;
import com.tuanzi.loan.business.service.statistics.ActivityParticipateStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.statistics.ActivityAverageJoinStatRequest;
import com.tuanzi.loan.vo.response.statistics.ActivityAverageJoinStatResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.ActivityAverageJoinStatistisLazyDataModel;
import com.tuanzi.server.loan_base.model.bean.common.CommAdvertise;
import com.tuanzi.server.loan_base.model.bean.common.CommPlatform;
import com.tuanzi.server.loan_base.model.bean.common.CommUserType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.joda.time.LocalDate;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 人均参与统计（友盟）
 * @author weibifeng 
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ActivityAverageJoinStatBean extends JSFListingBean<ActivityAverageJoinStatRequest, ActivityAverageJoinStatResponse> {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
    @Autowired
    private ActivityAverageJoinStatService activityAverageJoinStatService;
    @Autowired
    private ActivityParticipateStatService activityParticipateStatService;
    @Autowired
    private CommProductIdService commProductIdService;
    
    private StreamedContent xlsFile;

    private List<CommAdvertise> adList;
    private List<CommUserType> userTypeList;
    private List<CommPlatform> platformList;
    private TreeNode prodNodeTree;

    @PostConstruct
    private void init() {
        initList();
        initLazyDataModel();
    }

    private void initList(){
        adList = activityParticipateStatService.getAD();
        userTypeList = activityParticipateStatService.getUserType();
        platformList = activityParticipateStatService.getPlatform();
        prodNodeTree = commProductIdService.createProductTreeNode(null);
    }

    public void onFlushCache(){
        activityParticipateStatService.cleanCache();
        initList();
        addInfoMessage("缓存更新成功!");
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new ActivityAverageJoinStatistisLazyDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new ActivityAverageJoinStatRequest();
        
        // 初始化默认当前日期
        Date date = LocalDate.now().minusDays(1).toDate();
        $.setStartDate(date);
        $.setEndDate(date);
        
        // 初始化默认选择全部
        List<Integer> defaultInt = new ArrayList<>();
        defaultInt.add(-1000);
        $.setProductId(defaultInt);
        $.setUserType(defaultInt);
        $.setAdId(defaultInt);
        
        List<String> defaultStr = new ArrayList<String>();
        defaultStr.add("-1000");
        $.setPlatform(defaultStr);

        this.prodNodeTree.setSelected(true);
        
        lazyDataModel = new ActivityAverageJoinStatistisLazyDataModel($);
    }


    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.LARGE_COMMON_PRODUCT);
    }
}




